<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<view class="u-flex-col u-col-center money-box" >
			<text class="time">{{tit}}</text>
			<view class="money">{{price}}</view>
		</view>

		<!-- 支付方式单选项 -->
		<u-radio-group class="pay-box" v-model="payType"
			active-color="#f0c785">
			<!-- 支付宝支付 -->
			<view class="u-flex u-row-between pay-item"
				@tap="payType = 'alipay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMAGE_URL + '/order_ali_pay.png'" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<u-radio shape="circle" name="alipay"></u-radio>
			</view>
			<!-- 微信支付 -->
			<!-- <view class="u-flex u-row-between pay-item"
				@tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMAGE_URL + '/order_wx_pay.png'" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			 -->
			<!-- 苹果支付 -->
		<!-- 	<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('iospay') && appPlatfrom === 'ios'"
				@tap="payType = 'iospay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_apple_pay.png'" mode=""></image>
					<text>ApplePay</text>
				</view>
				<u-radio shape="circle" name="iospay"></u-radio>
			</view> -->
			<!-- 余额支付 -->
			<!-- <view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wallet')"
				@tap="payType = 'wallet'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wallet_pay.png'" mode=""></image>
					<text>余额支付</text>
				</view>
				<u-radio shape="circle" name="wallet"></u-radio>
			</view> -->
		</u-radio-group>
		<button class="u-reset-button pay-btn" @tap="confirmPay" :loading="isPay">确认支付 ￥{{ price || '0.00' }}</button>
	</view>
</template>

<script>
	/**
	 * 接收商品订单orderType:goods，充值订单orderType:recharge
	 */
	import Pay from '@/shopro/pay';
	import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
	let timer;
	export default {
		components: {},
		data() {
			return {
				payType: 'alipay', //支付方式
				isCountDown: true, //是否显示订单倒计时。
				isPay:false,
				orderDetail: {},
				timeText: '', //倒计时文本
				platform: this.$platform.get(),
				appPlatfrom: '',
				price: '',
				id:'',
				oid:'',
				tit:'',
				buyout:''
			};
		},
		computed: {
			...mapGetters(['initPayment', 'initRecharge']),
			
		},
		onShow() {
			// this.price && this.countDown();
		},
		onLoad() {
			this.price = this.$Route.query.price
			this.id = this.$Route.query.id
			this.oid = this.$Route.query.oid
			this.tit = this.$Route.query.tit
			this.buyout = this.$Route.query.buyout
			this.manyId = this.$Route.query.manyId
			
		},
		onHide() {
			clearInterval(timer);
			timer = null;
		},
		
		methods: {
			// 获取订单详情
			getOrderDetail() {
				switch (this.$Route.query.orderType) {
					case 'goods':
						this.getGoodsOrderDetail()
						break;
					case 'recharge':
						this.getRechargeOrderDetail()
						break;
					default:
						this.getGoodsOrderDetail()
						break;
				}
			},
			// 倒计时
			countDown() {
				let that = this;
				let t = parseInt(that.orderDetail.ext_arr.expired_time * 1000) - parseInt(new Date().getTime());
				t = t / 1000;
				timer = setInterval(() => {
					if (t > 0) {
						let time = that.$tools.format(t);
						that.timeText = `支付剩余时间 ${time.m}:${time.s}`;
						t--;
					} else {
						clearInterval(timer);
						that.timeText = '订单已过期!';
					}
				}, 1000);
			},
			jump(path, parmas) {
				  this.$Router.push({
					path: path,
					query: parmas,
				  });
			},
			// 发起支付
			confirmPay() {
				let that = this;
				let pay = null;
				if (!that.payType) {
					that.$u.toast('请选择支付方式');
					return;
				}
				if(that.isPay){
					return;
				}
				that.isPay = true;
				if (that.payType === 'wechat') {
					// uni.showModal({
					// 	title: '支付提示',
					// 	confirmColor: '#f0c785',
					// 	content: `是否确认使用微信支付:${that.price || '0.00'}元?`,
					// 	success: res => {
					// 		if (res.confirm) {
					// 			pay = new Pay(that.payType, that.orderDetail, that.$Route.query.orderType);
					// 		}
					// 	}
					// });
					if(that.buyout == 0 && that.manyId == 0){
						//单笔支付
						that.$http("goods.wxpay", {id:that.id,oid:that.oid}).then((pay) => {
							console.log('微信支付返回参数',pay)
							
							let obj = {
								appid: pay.data.appId,
								noncestr: pay.data.nonceStr,
								package: pay.data.packageValue,
								partnerid: pay.data.partnerId,
								prepayid: pay.data.prepayId,
								timestamp: pay.data.timeStamp,
								sign: pay.data.sign,
							};
							console.log('微信支付返回参数',obj)
							uni.requestPayment({
								provider: "wxpay",
								orderInfo: obj,
								success(res) {
									that.$u.toast('支付成功！');
									that.isPay = false;
									uni.switchTab({
										url: '/pages/index/user'
									});
								},
								fail(e) {
									that.isPay = false;
									that.$u.toast('支付失败！');
								},
								complete() {
									console.log("啥也没干");
								}
							});
						})
					}else if(that.buyout == 0 && that.manyId != 0){
						//提前还款
						that.$http("goods.wxpays", that.manyId).then((pay) => {
							console.log('微信支付返回参数',pay)
							if(pay.code==500){
								that.$u.toast("选择期数过多请使用支付宝支付！");
								return ;
							}
							let obj = {
								appid: pay.data.appId,
								noncestr: pay.data.nonceStr,
								package: pay.data.packageValue,
								partnerid: pay.data.partnerId,
								prepayid: pay.data.prepayId,
								timestamp: pay.data.timeStamp,
								sign: pay.data.sign,
							};
							console.log('微信支付返回参数',obj)
							uni.requestPayment({
								provider: "wxpay",
								orderInfo: obj,
								success(res) {
									that.$u.toast('支付成功！');
									that.isPay = false;
									uni.switchTab({
										url: '/pages/index/user'
									});
								},
								fail(e) {
									that.isPay = false;
									that.$u.toast('支付失败！');
								
								},
								complete() {
									console.log("啥也没干");
								}
							});
						})
					}else if(that.buyout == 1){
						that.isPay = false;
						//买断款
						that.$u.toast('买断款请使用支付宝支付！');
					// that.$http("goods.wxpayBuyOut", {oid:that.oid}).then((pay) => {
					// 	console.log('微信支付返回参数',pay)
						
					// 	let obj = {
					// 		appid: pay.data.appId,
					// 		noncestr: pay.data.nonceStr,
					// 		package: pay.data.packageValue,
					// 		partnerid: pay.data.partnerId,
					// 		prepayid: pay.data.prepayId,
					// 		timestamp: pay.data.timeStamp,
					// 		sign: pay.data.sign,
					// 	};
					// 	console.log('微信支付返回参数',obj)
					// 	uni.requestPayment({
					// 		provider: "wxpay",
					// 		orderInfo: obj,
					// 		success(res) {
					// 			that.$u.toast('支付成功！');
					// 			that.jump('/pages/order/list', { type: '-2' })
					// 		},
					// 		fail(e) {
					// 			that.$u.toast('支付失败！');
							
					// 		},
					// 		complete() {
					// 			console.log("啥也没干");
					// 		}
					// 	});
					// })
						
					}
					
						// uni.requestPayment({
						// 	provider: 'alipay',
						// 	orderInfo: pay.data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
						// 	success: function (res) {
						// 		that.$u.toast('支付成功！');
						// 		let productOrderRecordVoList = that.productOrderRecordVoList;
						// 		productOrderRecordVoList[index].payment = 1;
						// 		that.productOrderRecordVoList = productOrderRecordVoList;
						// 		that.jump('/pages/order/list', { type: '-2' })
						// 	},
						// 	fail: function (err) {
						// 		that.$u.toast('支付失败！');
						// 		console.log('fail:' + JSON.stringify(err));
						// 	}
						// });
									
				} else {
					console.log("支付宝支付")
					// uni.showModal({
					// 	title: '支付提示',
					// 	confirmColor: '#f0c785',
					// 	content: `是否确认使用支付宝支付:${that.price || '0.00'}元?`,
					// 	success: res => {
					// 		if (res.confirm) {
					// 			that.$http("goods.alipay", {id:that.id,oid:that.oid}).then((pay) => {
					// 				console.log('支付宝支付',pay)
					// 				uni.requestPayment({
					// 					provider: 'alipay',
					// 					orderInfo: pay.data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
					// 					success: function (res) {
					// 						that.$u.toast('支付成功！');
					// 						let productOrderRecordVoList = that.productOrderRecordVoList;
					// 						productOrderRecordVoList[index].payment = 1;
					// 						that.productOrderRecordVoList = productOrderRecordVoList;
					// 						that.jump('/pages/order/list', { type: '-2' })
					// 					},
					// 					fail: function (err) {
					// 						that.$u.toast('支付失败！');
					// 						console.log('fail:' + JSON.stringify(err));
					// 					}
					// 				});
												
					// 			})
					// 		}
					// 	}
					// });
					if(that.buyout == 0 && that.manyId == 0){
						//单笔支付
						that.$http("goods.alipay", {id:that.id,oid:that.oid}).then((pay) => {
							console.log('支付宝支付',pay)
							if(pay.code != 200){
								that.$u.toast(pay.msg);
								that.isPay = false;
								return ;
							}
							window.location.href = pay.data;
							// uni.requestPayment({
							// 	provider: 'alipay',
							// 	orderInfo: pay.data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
							// 	success: function (res) {
							// 		that.$u.toast('支付成功！');
							// 		that.isPay = false;
							// 		uni.switchTab({
							// 			url: '/pages/index/user'
							// 		});
							// 	},
							// 	fail: function (err) {
							// 		that.isPay = false;
							// 		that.$u.toast('支付失败！');
							// 		console.log('fail:' + JSON.stringify(err));
							// 	}
							// });
										
						})
					}else if(that.buyout == 0 && that.manyId != 0){
						//提前还款
						that.$http("goods.alipayRecords",that.manyId).then((pay) => {
							if(pay.code != 200){
								that.$u.toast(pay.msg);
								that.isPay = false;
								return ;
							}
							window.location.href = pay.data;
							// uni.requestPayment({
							// 	provider: 'alipay',
							// 	orderInfo: pay.data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
							// 	success: function (res) {
							// 		that.isPay = false;
							// 		that.$u.toast('支付成功！');
							// 		uni.switchTab({
							// 			url: '/pages/index/user'
							// 		});
							// 	},
							// 	fail: function (err) {
							// 		that.isPay = false;
							// 		that.$u.toast('支付失败！');
							// 		console.log('fail:' + JSON.stringify(err));
							// 	}
							// });
										
						})
					}else if(that.buyout == 1){
						//买断款
						that.$http("order.buyout", {oid:that.oid}).then((pay) => {
							console.log('支付宝支付',pay)
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: pay.data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
								success: function (res) {
									that.isPay = false;
									that.$u.toast('支付成功！');
									uni.switchTab({
										url: '/pages/index/user'
									});
								},
								fail: function (err) {
									that.isPay = false;
									that.$u.toast('支付失败！');
									console.log('fail:' + JSON.stringify(err));
								}
							});
										
						})
					}
				
				}
			},

			// 支付商品订单信息
			getGoodsOrderDetail() {
				let that = this;
				that.$http('order.detail', {
					id: that.$Route.query.orderId
				}).then(res => {
					if (res.code === 1) {
						that.orderDetail = res.data;
						if (res.data.ext_arr !== null) {
							that.countDown();
						} else {
							that.isCountDown = false;
						}
					}
				});
			},

			// 充值订单信息
			getRechargeOrderDetail() {
				let that = this;
				that.$http('money.rechargeDetail', {
					id: that.$Route.query.orderId
				}).then(res => {
					if (res.code === 1) {
						that.orderDetail = res.data;
						if (res.data.ext_arr !== null) {
							that.countDown();
						} else {
							that.isCountDown = false;
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.money-box {
		background: #fff;
		height: 250rpx;
		margin-bottom: 20rpx;
		padding-top: 30rpx;

		.time {
			font-size: 28rpx;
			color: #c4c4c4;
		}

		.money {
			color: #e1212b;
			font-size: 60rpx;
			margin-top: 60rpx;

			&::before {
				content: '￥';
				font-size: 46rpx;
			}
		}
	}

	.pay-box {
		.pay-item {
			height: 90rpx;
			padding: 0 30rpx;
			font-size: 26rpx;
			background: #fff;
			width: 750rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border-bottom: none;
			}

			.pay-img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 25rpx;
			}
		}
	}

	.pay-btn {
		width: 690rpx;
		line-height: 80rpx;
		background: linear-gradient(90deg, rgba(240, 199, 133, 1), rgba(246, 214, 157, 1));
		border-radius: 40rpx;
		color: rgba(#fff, 0.9);
		margin: 100rpx auto 0;
	}
</style>
